package com.test;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class FileLockExample2 {
    public static void main(String[] args) {

        File file = new File("example.txt");

        synchronized (file){
            try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
                String line;
                while((line = bufferedReader.readLine()) != null){
                    System.out.println(line);
                }
                Thread.sleep(10000);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        new Thread(() ->{
            try (BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("example.txt")))){
                String line;
                while((line = bufferedReader.readLine()) != null){
                    System.out.println(line);
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).start();





    }
}